/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.system.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 *@Description 实现ApplicationContextAware接口，提供获取spring上下文容器和对应的容器中的对象
 *@Author 操文健
 *@Date 2018-09-06 上午10:13
 *@Version 1.0
 **/
@Component
public class ApplicationContextComponent implements ApplicationContextAware {

    private static Logger logger = LoggerFactory.getLogger(ApplicationContextComponent.class);

    // spring Context
    private static ApplicationContext localApplicationContext;
    /**
     * 重写该方法，设置spring上下文
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info("InitSpring ApplicationContext  ->  ", applicationContext);
        localApplicationContext = applicationContext;
    }

    /**
     * 提供一个静态方法用来获取spring容器对象
     * @return
     */
    public static ApplicationContext getApplicationContext () {
        return localApplicationContext;
    }

    /**
     * 提供返回容器中autowired的对象
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return localApplicationContext.getBean(clazz);
    }
}
